﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using PPAP.BO;
using System.Data;

namespace PPAP.Web.Supplier
{
    public partial class SupplierManage : PPAP.Web.PageBase
    {
        //protected System.Web.UI.WebControls.TextBox txtName;
        //protected System.Web.UI.WebControls.Button btnSearch;
        //protected System.Web.UI.WebControls.TextBox txtInsuranceNum;
        //protected System.Web.UI.WebControls.DataGrid dgList;
        //protected PPAP.WebControls.CompanySelectDropList CompanySelectDropList1;
        //protected Beyondbit.CService.WebControls.TPager pager;
        //protected Wuqi.Webdiyer.AspNetPager AspNetPager2;

        SupplierBO bo = new SupplierBO();
        LogBO lbo = new LogBO();
        private string Type;
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                bindCompany();
            }
            if (Request.QueryString["deid"] != null)
            {
                dele(Request.QueryString["deid"].ToString());
            }
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="id"></param>
        private void dele(string id)
        {
            int num = bo.DeleteSupplier(int.Parse(id));
            lbo.WriteLog(CurrentUser.Id, "Delete Supplier");
            if (num > 0)
                AlertMessage("Deleted successful");
            if (num == -1)
            {
                AlertRedirect("The supplier do not delete, because it has the 'parts' information.","SupplierManage.aspx");
            }
            bindCompany();
        }



        #region Web 窗体设计器生成的代码
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// 设计器支持所需的方法 - 不要使用代码编辑器修改
        /// 此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.btnSearch.Click += new System.EventHandler(this.btnSearch_Click);
            //this.pager.PagerChanged += new Beyondbit.CService.WebControls.TPager.PageChangedEventHandler(this.pager_PagerChanged);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void bindCompany()
        {
            bindData(true);
            //this.CompanySelectDropList1.Items.Clear() ;
            //SupplierBO bo = new SupplierBO();

            //DataTable dt = bo.GetList("").Tables[0];
            //ListItem item;
            //item = new ListItem("全部供应商", "0");
            //this.CompanySelectDropList1.Items.Add(item);
            //foreach (System.Data.DataRow row in dt.Rows)
            //{
            //    item = new ListItem(row["SUPPLIERNAME"].ToString()+" ["+row["username"].ToString()+"]", row["ID"].ToString());
            //    this.CompanySelectDropList1.Items.Add(item);
            //}
        }

        private void btnSearch_Click(object sender, System.EventArgs e)
        {
            //pager.CurrentPageIndex = 0;
            bindData(true);
        }
        public void bindData(bool getcount)
        {

            string sqlwhere = "";
            if (txtName.Text != "")
                sqlwhere += " and lower(suppliername) like lower('%" + txtName.Text + "%') ";
            if (txtInsuranceNum.Text != "")
                sqlwhere += " and lower(suppliercode) like lower('%" + txtInsuranceNum.Text + "%') ";

            DataSet dt = bo.GetList(sqlwhere);
            AspNetPager2.RecordCount = dt.Tables[0].Rows.Count;
            dgList.DataSource = GetPageTable(dt.Tables[0], AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            this.dgList.DataBind();
        }

        private void UnitDropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            bindCompany();
        }

        private void pager_PagerChanged(object sender, Beyondbit.CService.WebControls.PageChangedEventArgs e)
        {
            bindData(false);
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            bindData(false);
        }

        /// <summary>
        /// 排序
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        protected void dgList_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            // 从事件参数获取排序数据列
            string sortExpression = e.SortExpression.ToString();

            // 假定为排序方向为“顺序”
            string sortDirection = "ASC";
            // “ASC”与事件参数获取到的排序方向进行比较，进行GridView排序方向参数的修改
            if (sortExpression == this.dgList.Attributes["SortExpression"])
            {
                //获得下一次的排序状态
                sortDirection = (this.dgList.Attributes["SortDirection"].ToString() == sortDirection ? "DESC" : "ASC");
            }
            // 重新设定GridView排序数据列及排序方向
            this.dgList.Attributes["SortExpression"] = sortExpression;
            this.dgList.Attributes["SortDirection"] = sortDirection;


            string pic = "";
            pic = sortDirection == "ASC" ? "<img src='../Images/icon/arrowup.gif' />" : "<img src='../Images/icon/arrowdown.gif' />";

            foreach (DataGridColumn item in dgList.Columns)
            {
                if (item.SortExpression == sortExpression)
                {
                    item.HeaderText = item.FooterText + " " + pic;
                }
                else
                    item.HeaderText = item.FooterText;
            }

            this.BindGridView();

        }

        private void BindGridView()
        {
            // 获取GridView排序数据列及排序方向
            string sortExpression = this.dgList.Attributes["SortExpression"];
            string sortDirection = this.dgList.Attributes["SortDirection"];
            // 调用业务数据获取方法

            string sqlwhere = "";
            if (txtName.Text != "")
                sqlwhere += " and suppliername like '%" + txtName.Text + "%'";
            if (txtInsuranceNum.Text != "")
                sqlwhere += " and suppliercode like '%" + txtInsuranceNum.Text + "%'";

             //bo.GetList(sqlwhere);
             DataTable dtBind = bo.GetList(string.Format(sqlwhere+"  order by lower({0})  {1} ", sortExpression, sortDirection)).Tables[0];
            // 根据GridView排序数据列及排序方向设置显示的默认数据视图
            if ((!string.IsNullOrEmpty(sortExpression)) && (!string.IsNullOrEmpty(sortDirection)))
            {
                dtBind.DefaultView.Sort = string.Format("{0} {1}", sortExpression, sortDirection);
            }
            // GridView绑定并显示数据
            this.dgList.DataSource = GetPageTable(dtBind, AspNetPager2.PageSize, AspNetPager2.CurrentPageIndex);
            this.dgList.DataBind();
        }
    }
}